package com.axon.dataservices.helpers {
  import com.axon.base.managers.AxWaitManager;

  public class AxDSAsyncResponder {
    //////////////////////////////////////////////////////////////////////////////////////////////////////

    private var fCallbackSuccess:Function;

    private var fCallbackFailure:Function;

    private var fHandleWaitCursor:Boolean;

    private var fHandled:Boolean = false;

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    public function AxDSAsyncResponder(callbackSuccess:Function, callbacFailure:Function) {
      fCallbackSuccess = callbackSuccess;
      fCallbackFailure = callbacFailure;
      fHandleWaitCursor = ((callbackSuccess != null) || (callbacFailure != null));

      if (fHandleWaitCursor)
        AxWaitManager.beginWaitMode();
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    public function handleSuccess(result:Object):void {
      if (fHandleWaitCursor)
        AxWaitManager.endWaitMode();
      if (fCallbackSuccess != null && !fHandled) {
        fHandled = true;
        fCallbackSuccess(result);
      }
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////

    public function handleFailure(result:Object):void {
      if (fHandleWaitCursor)
        AxWaitManager.endWaitMode();

      if (fCallbackFailure != null && !fHandled) {
        fHandled = true;
        fCallbackFailure(result);
      }
    }

  }
}